package com.imti.retrofitweatherdemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.imti.retrofitweatherdemo.NewsListAdapter.NewsListHolder;
import com.imti.retrofitweatherdemo.logic.NewsModelListItem;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class NewsListAdapter extends RecyclerView.Adapter<NewsListHolder>
{

    private Context context;

    private List<NewsModelListItem> newsModelLists;

    public NewsListAdapter(Context context)
    {
        this.context = context;
    }

    public void setNewsModelLists(
        List<NewsModelListItem> newsModelListItems)
    {
        this.newsModelLists = newsModelListItems;
    }
    @NonNull
    @Override
    public NewsListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        return new NewsListHolder(
            LayoutInflater.from(context).inflate(R.layout.news_list_item, null, false));
    }
    @Override
    public void onBindViewHolder(@NonNull NewsListAdapter.NewsListHolder holder, int position)
    {
        if (null != newsModelLists.get(position).getThumbPath())
        {
//            try
//            {
//            //下载图片
//                FutureTarget<File> target = Glide.with(context)
//                                                //asFile()设置返回为文件
//                                                .asFile()
//                                                .load(newsModelLists.get(position).getThumbPath())
//                                                //设置下载监听
//                                                .listener(new RequestListener<File>()
//                                                {
//                                                    @Override
//                                                    public boolean onLoadFailed(
//                                                        @Nullable  GlideException e,
//                                                        Object model, Target<File> target,
//                                                        boolean isFirstResource)
//                                                    {
//                                                        return false;
//                                                    }
//                                                    @Override
//                                                    public boolean onResourceReady(File resource,
//                                                        Object model, Target<File> target,
//                                                        DataSource dataSource,
//                                                        boolean isFirstResource)
//                                                    {
//                                                        ((Activity) context).runOnUiThread(
//                                                            new Runnable()
//                                                            {
//                                                                @Override
//                                                                public void run()
//                                                                {
//                                                                    if (null != resource)
//                                                                    {
//                                                                        Toast.makeText(context,
//                                                                            "图片下载完成",
//                                                                            Toast.LENGTH_SHORT)
//                                                                            .show();
//                                                                    }
//                                                                }
//                                                            });
//
//                                                        return false;
//                                                    }
//                                                })
//                                                //开启图片下载
//                                                .submit();
//                File imageFile = target.get();
//
//                ((Activity) context).runOnUiThread(new Runnable()
//                {
//                    @Override
//                    public void run()
//                    {
//                        holder.itemImage
//                            .setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
//                    }
//                });
//
//            } catch (ExecutionException e)
//            {
//                e.printStackTrace();
//            } catch (InterruptedException e)
//            {
//                e.printStackTrace();
//            }

            //with()方法创建对象
            Glide.with(context)
                //load()方法加载URL
                .load(newsModelLists.get(position).getThumbPath())
                //into()方法显示图片
                .into(holder.itemImage);
        } else
        {
            holder.itemImage.setImageResource(R.mipmap.ic_launcher);
        }

        holder.itemTitle.setText(newsModelLists.get(position).getTitle());
        holder.itemTime.setText(newsModelLists.get(position).getTime());

        holder.itemView.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent((MainActivity) context, NewsDetailActivity.class);

                intent.putExtra("news_id", newsModelLists.get(position).getId());

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount()
    {
        return newsModelLists.size();
    }

    class NewsListHolder extends ViewHolder
    {

        ImageView itemImage;

        TextView itemTitle;

        TextView itemTime;

        public NewsListHolder(@NonNull View itemView)
        {
            super(itemView);

            itemImage = itemView.findViewById(R.id.news_list_item_image);
            itemTitle = itemView.findViewById(R.id.news_list_item_title);
            itemTime = itemView.findViewById(R.id.news_list_item_time);
        }
    }
}
